using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using Weazel.Toad.Application.Actions;

namespace Weazel.Toad.Application.Controls
{
  public partial class DefaultLayoutControl : UserControl
  {
    private ToolStripMenuItem closeToolStripMenuItem;

    public TabControl MainTabControl
    {
      get
      {
        return this.mainTabControl;
      }
    }

    public GezelModelExplorer GezelModelExplorer
    {
      get
      {
        return gezelModelExplorer;
      }
    }

    public ProjectExplorer ProjectExplorer
    {
      get
      {
        return null;
      }
    }

    public ContainerPanel ContainerPanel
    {
      get
      {
        return this.containerPanel;
      }
    }

    public DefaultLayoutControl()
    {
      InitializeComponent();

      // setup main tabcontrol context menu
      mainTabControl.ContextMenuStrip = new ContextMenuStrip();
      closeToolStripMenuItem = new ToolStripMenuItem();
      closeToolStripMenuItem.Text = "Close this file";
      mainTabControl.ContextMenuStrip.Items.Add(closeToolStripMenuItem);

      Application.Instance.SelectedItemChanged += 
        new EventHandler<SelectedItemChangedEventArgs>(Instance_SelectedItemChanged);
    }

    void Instance_SelectedItemChanged(object sender, SelectedItemChangedEventArgs e)
    {
      if (e.SelectedItem != null && e.SelectedItem.TabPage != mainTabControl.SelectedTab)
        mainTabControl.SelectedTab = e.SelectedItem.TabPage;
    }

    private void mainTabControl_SelectedIndexChanged(object sender, EventArgs e)
    {
      if (mainTabControl.SelectedIndex >= 0)
      {
        // we have more tab pages, select item from new selected tab page
        TabPage selectedPage = mainTabControl.TabPages[mainTabControl.SelectedIndex];
        Application.Instance.SelectedItem = (Items.ItemBase)selectedPage.Tag;
      }
      else
      {
        // no more tab pages -> no selected item
        Application.Instance.SelectedItem = null;
      }
    }

    public void BindActions()
    {
      ActionBase.Actions[typeof(CloseFileAction)].BindToolStripItem(closeToolStripMenuItem);
    }
  }
}
